import { getEmrType } from '@/api/modules/emr'
import { docTypeDefaultWithMedicine } from '@/api/modules/actionPlan'
import store from '..'

const state = {
  emrTypes: null,
  emrActivityType: null,
  // 是否在编辑
  isEditing: false,
  // 设置默认药品数据
  defaultMedicines: {},
  emrAcuteId: null,
  emrControlId: null,
  controlDialogStatus: false,
  acuteDialogStatus: false,
  emrRecordBirthday: null,
}

const mutations = {
  SET_EMRTYPES: (state, types) => {
    state.emrTypes = types
  },
  SET_ACTIVITYEMRTYPE: (state, emrType) => {
    state.emrActivityType = emrType
  },
  SET_EDITINGSTATUS: (state, isEditing) => {
    state.isEditing = isEditing
  },
  SET_DEFAULTMEDICINE: (state, defaultMedicines) => {
    state.defaultMedicines = defaultMedicines
  },
  SET_EMRACUTEID: (state, emrAcuteId) => {
    state.emrAcuteId = emrAcuteId
  },
  SET_EMRCONTROLID: (state, emrControlId) => {
    state.emrControlId = emrControlId
  },
  // 清理id
  SET_CLEARCONTROLIDANDACUTEID: (state) => {
    state.emrAcuteId = null
    state.emrControlId = null
  },
  // 设置控制水平弹窗
  SET_CONTROL_DIALOG_STATUS: (state, status) => {
    state.controlDialogStatus = status
  },
  // 设置急性弹窗
  SET_ACUTE_DIALOG_STATUS: (state, status) => {
    state.acuteDialogStatus = status
  },
  SET_EMR_RECORD_BIRTHDAY: (state, date) => {
    state.emrRecordBirthday = date
  },
}

const actions = {
  loadEmrTypes({ commit }) {
    return new Promise((resolve, reject) => {
      getEmrType({ type: 'emr' })
        .then((res) => {
          const types = res.data.map((item) => {
            return { value: item.value, label: item.key }
          })
          types.sort((a, b) => {
            return a.label === '新哮喘病历' ? -1 : 0
          })
          resolve(types)
          commit('SET_EMRTYPES', types)
        })
        .catch(() => {
          reject()
        })
    })
  },
  setActivityEmrType({ commit }, emrType) {
    return new Promise((resolve) => {
      commit('SET_ACTIVITYEMRTYPE', emrType)
      resolve()
    })
  },
  setEditingStatus({ commit }, isEditing = false) {
    return new Promise((resolve) => {
      commit('SET_EDITINGSTATUS', isEditing)
      resolve()
    })
  },
  // 目前设置只调用一次 后期考虑建设长期缓存机制
  /**
   * 获取默认药物
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  getDefaultMedicine({ commit }, params = {}) {
    if (store.defaultMedicines) {
      Promise.resolve(store.defaultMedicines)
      return
    }
    return new Promise((resolve, reject) => {
      docTypeDefaultWithMedicine(params)
        .then((res) => {
          commit('SET_DEFAULTMEDICINE', res.data)
          resolve(res.data)
        })
        .catch((err) => {
          console.error(err)
          reject()
        })
    })
  },
  /**
   * 设置三个来源 1. 病历加载过来的，< 2. 点击诊断更新 < 3. 做筛查评估时
   * setEmrAcuteId 设置急性订单id
   * setControlId 设置控制水平弹窗
   */
  setEmrAcuteId({ commit }, { enumType, id }) {
    commit('SET_EMRACUTEID', id)
  },
  setControlId({ commit }, { enumType, id }) {
    // if (enumType === 3) {
    //   console.log('ControlId 3')
    //   commit('SET_EMRCONTROLID', id)
    // } else if (enumType === 2) {
    //   console.log('ControlId 2')
    //   commit('SET_EMRCONTROLID', id)
    // } else if (enumType === 1) {
    //   console.log('ControlId 1')
    //   commit('SET_EMRCONTROLID', id)
    // }
    commit('SET_EMRCONTROLID', id)
  },
  clearControlIdAndAcuteId({ commit }) {
    console.log('clearControlIdAndAcuteId 清理')
    commit('SET_CLEARCONTROLIDANDACUTEID')
  },
  // 决定是否显示
  setControlDialogStatus({ commit }, status = false) {
    commit('SET_CONTROL_DIALOG_STATUS', status)
  },
  setAcuteDialogStatus({ commit }, status = false) {
    commit('SET_ACUTE_DIALOG_STATUS', status)
  },
  setEmrRecordBirthday({ commit }, recordBirthday = null) {
    commit('SET_EMR_RECORD_BIRTHDAY', recordBirthday)
  },
}

export default {
  namespaed: true,
  state,
  mutations,
  actions,
}
